package com.css.fxfzxczh.modules.buildingKilometerGrid.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.modules.buildingKilometerGrid.model.entity.FxqhBuildingGridFieldDesc;
import com.css.fxfzxczh.modules.buildingKilometerGrid.repository.FxqhBuildingGridFieldDescRepository;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("test")
@RestController
public class TestController {

    @Autowired
    FxqhBuildingGridFieldDescRepository fxqhBuildingGridFieldDescRepository;

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    static final String[] typeArr = {
            "czghjggw",
            "czgjggw",
            "czmjggw",
            "czqtjggw",
            "czqtjglxgw",
            "ncdbkjkzqqtjggw",
            "ncghjggw",
            "ncgjggw",
            "nchzjggw",
            "ncmzjggw",
            "ncqtjggw",
            "ncqtjglxgw",
            "nctmsmjggw"
    };

    @GetMapping("fieldRemarkAddRangeInfo")
    public String fieldRemarkAddRangeInfo() {
        List<FxqhBuildingGridFieldDesc> dtos = new ArrayList<>();
        for (String type : typeArr) {
            List<FxqhBuildingGridFieldDesc> list_ss = new ArrayList<>();
            String sql = "select * from fxqh_building_grid_field_desc_copy1 where build_grid_type = '" + type + "' order by sort asc";
            List<FxqhBuildingGridFieldDesc> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FxqhBuildingGridFieldDesc.class));
            int i = 1;
            list_ss.add(getFxqhBuildingGridFieldDesc("OBJECTID", "浮点型", "是", i++, type));
            list_ss.add(getFxqhBuildingGridFieldDesc("OID_1", "浮点型", "是", i++, type));
            list_ss.add(getFxqhBuildingGridFieldDesc("ROWS", "浮点型", "是", i++, type));
            list_ss.add(getFxqhBuildingGridFieldDesc("COLUMS", "浮点型", "是", i++, type));
            list_ss.add(getFxqhBuildingGridFieldDesc("AREA_CODE", "文本型", "是", i++, type));
            list_ss.add(getFxqhBuildingGridFieldDesc("SHAPE_LENG", "浮点型", "是", i++, type));
            list_ss.add(getFxqhBuildingGridFieldDesc("SHAPE_LE_1", "浮点型", "是", i++, type));
            list_ss.add(getFxqhBuildingGridFieldDesc("SHAPE_AREA", "浮点型", "是", i++, type));
            for (FxqhBuildingGridFieldDesc fxqhBuildingGridFieldDesc : list) {
                fxqhBuildingGridFieldDesc.setSort(i++);
                list_ss.add(fxqhBuildingGridFieldDesc);
            }
            list_ss.add(getFxqhBuildingGridFieldDesc("省名称", "文本型", "是", i++, type));
            list_ss.add(getFxqhBuildingGridFieldDesc("省CODE", "文本型", "是", i++, type));
            list_ss.add(getFxqhBuildingGridFieldDesc("市名称", "文本型", "是", i++, type));
            list_ss.add(getFxqhBuildingGridFieldDesc("市CODE", "文本型", "是", i++, type));
            list_ss.add(getFxqhBuildingGridFieldDesc("县名称", "文本型", "是", i++, type));
            list_ss.add(getFxqhBuildingGridFieldDesc("县CODE", "文本型", "是", i++, type));
            System.out.println(list_ss);
            dtos.addAll(list_ss);
        }

        String sql = "delete from fxqh_building_grid_field_desc";
        jdbcTemplate.update(sql);

        for (FxqhBuildingGridFieldDesc dto : dtos) {
            fxqhBuildingGridFieldDescRepository.insert(dto);
        }
        return "OK";
    }

    private FxqhBuildingGridFieldDesc getFxqhBuildingGridFieldDesc(String dataName, String dataType, String isMust, int sort, String gridType) {
        FxqhBuildingGridFieldDesc province = new FxqhBuildingGridFieldDesc();
        province.setId(IdUtil.getSnowflakeNextIdStr());
        province.setDataName(dataName);
        province.setDataType(dataType);
        province.setIsMust(isMust);
        province.setSort(sort);
        province.setBuildGridType(gridType);
        return province;
    }

//    @PostMapping
//    public String test() throws IOException {
//        this.readTableData("C:\\Users\\罗燕师\\Desktop\\地震灾害风险评估与区划系统\\2023-04012-高永武-住建房屋建筑格网数据制作数据字典(2)(1)(2).docx");
//        return "ok";
//    }

//    public static void main(String[] args) throws IOException {
//        readTableData("C:\\Users\\罗燕师\\Desktop\\地震灾害风险评估与区划系统\\2023-04012-高永武-住建房屋建筑格网数据制作数据字典(2)(1)(2).docx");
//    }

    public void readTableData(String path) throws IOException {
        int i = 1;
        InputStream is = new FileInputStream(path);
        XWPFDocument doc = new XWPFDocument(is);
        XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
        String text = extractor.getText();
        String[] nodeSplit = text.split("\n" +
                "\n" +
                "\n");
        List<JSONObject> a = new ArrayList<>();
        for (String node : nodeSplit) {
            JSONObject jsonObject = new JSONObject();
            if (StringUtils.isBlank(StringUtils.trim(node))) {
                continue;
            }
            String[] bodySplit = node.split("序号\t字段\t字段说明\t数据类型\t备注");
            String title = bodySplit[0];
            if (title.split("\n").length > 1) {
                title = title.split("\n")[0];
            }
            title = StringUtils.trim(title);
            String bodyText = bodySplit[1];
            String[] bodyRows = bodyText.split("\n");
            List<JSONObject> nodes = new ArrayList<>();
            for (String row : bodyRows) {
                if (StringUtils.isBlank(row)) {
                    continue;
                }
                String[] split = row.split("\t");
                String dataName = split[2];
                dataName = StringUtils.trim(dataName);
                String dataType = split[3];
                if (dataType.contains("numeric")) {
                    dataType = "浮点型";
                }
                if (dataType.contains("int")) {
                    dataType = "整型";
                }
                String isMust = "是";
                String dataDesc = "";
                JSONObject nodeJson = new JSONObject();
                nodeJson.put("dataName", dataName);
                nodeJson.put("dataType", dataType);
                nodeJson.put("isMust", isMust);
                nodeJson.put("dataDesc", dataDesc);
                FxqhBuildingGridFieldDesc fxqhBuildingGridFieldDesc = new FxqhBuildingGridFieldDesc();
                fxqhBuildingGridFieldDesc.setId(IdUtil.getSnowflakeNextIdStr());
                fxqhBuildingGridFieldDesc.setDataType(dataType); // 数据类型
                fxqhBuildingGridFieldDesc.setIsMust(isMust); // 是否必填
                fxqhBuildingGridFieldDesc.setDataName(dataName); // 数据名称
                fxqhBuildingGridFieldDesc.setDataDesc(dataDesc); // 数据说明
                fxqhBuildingGridFieldDesc.setBuildGridType(title); // 类别
                fxqhBuildingGridFieldDesc.setSort(i++); // 排序
                fxqhBuildingGridFieldDescRepository.insert(fxqhBuildingGridFieldDesc);
                nodes.add(nodeJson);
            }
            jsonObject.put("title", title);
            jsonObject.put("fieldDesc", nodes);
            a.add(jsonObject);
        }
//        System.out.println(JSONObject.toJSONString(jsonObject, SerializerFeature.PrettyFormat));
//        for (JSONObject jsonObject : a) {
//            jsonObject.get()
//        }

        is.close();
    }
}
